//
//  WCNLoginVC.m
//  WCN
//
//  Created by 杨鑫 on 15/5/29.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//


#import "BTLoginVC.h"
//VC

#import "NSString+Utilities.h"
#import "WCBorderView.h"
#import "HELTabBarVC.h"
#import "HELNavigationController.h"
#import "AppDelegate.h"
#import <UMMobClick/MobClick.h>
#import "WCBaiduPushBindHelp.h"
#import "NCForgetPwdVC.h"

@interface BTLoginVC () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation BTLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"登录"];
  
    self.pwdTextField.delegate = self;
//   self.phoneTextField.text = @"200831002067";
//    self.pwdTextField.text = @"123456";
//    self.phoneTextField.text = [HELAppManager userInfo];
  
}

#pragma mark - Nib Targets
//click backgroundView
- (IBAction)bgTouchDown:(id)sender
{
    
//    [self.phoneTextField hideList];
    [self.view endEditing:YES];
}

//忘记密码
- (IBAction)showPwdVC:(id)sender {
    NCForgetPwdVC *pwdVC = [NCForgetPwdVC new];
 
    [self.navigationController pushViewController:pwdVC animated:YES];
}

// 点击登录
- (IBAction)doLogin:(id)sender {

  
    [self.view endEditing:YES];
    NSString *userAcount = self.phoneTextField.text, *password = self.pwdTextField.text;
    if ([userAcount isEmpty]) {
        cwqAlertMessage(@"账号不能为空～");
        return;
    }

    if ([password isEmpty]) {
        cwqAlertMessage(@"密码不能为空～");
        return;
    }
    
    cwqHud(self.view);
    hud.detailsLabelText  =@"登录中..";
    weakSelf_define;
  
  
    NSDictionary *param = @{@"account":userAcount, @"password":password};
    [[HELNetworkManager sharedMg] POST_Noken:helLoginUrl parameters:param success:^(id responseObject) {
         [hud hide:YES];
        
        if ([responseObject[@"success"] isKindOfClass:[NSNumber class]] && !responseObject[@"success"]) {

           
        }else {
            
            [HELAppManager saveToken:responseObject[@"data"][@"xauthToken"]];
            [HELAppManager saveUseInfo:responseObject[@"data"][@"user"]];
            //            [MobClick profileSignInWithPUID:responseObject[@"user"][@"userId"]];
            [MobClick event:@"LoginCount" label:deviceName];
            
            cwqHudSuccess(@"登陆成功!");
                       //百度云推绑定后台
//            [WCBaiduPushBindHelp bindToService];
            [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccessNotification object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:BindPhoneNotification object:nil];
            
            [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
        
    } failure:^(NSString *err) {
         [hud hide:YES];
        cwqHudRequestError(err);
    }];
}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}




- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField.tag == 0) {
        self.pwdTextField.text = @"";
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1) {
        [textField resignFirstResponder];
        [self doLogin:nil];
        return YES;
    }
    
    return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag ==0) {
//        [self showSuggestList];
    }
}
@end
